# Makefile
nullstring :=
space := $(nullstring) $(nullstring)#end of the line
VS2019INSTALLDIR = C:\Program$(space)Files$(space)(x86)\Microsoft$(space)Visual$(space)Studio\2019\Community
oneAPI_HOME = C:\Program$(space)Files$(space)(x86)\Intel\oneAPI
oneAPI_PATH = $(oneAPI_HOME)\compiler\2023.2.0\windows
oneMKL_HOME = $(oneAPI_HOME)\mkl\2023.2.0
VS_LIBS_HOME = $(VS2019INSTALLDIR)\VC\Tools\MSVC\14.29.30133\lib\x64
CUDA_HOME = C:\Program$(space)Files\NVIDIA$(space)GPU$(space)Computing$(space)Toolkit\CUDA\v12.3

oneAPI_LIBS_HOME = $(oneAPI_PATH)\compiler\lib\intel64_win
LINK_LIB = "$(oneAPI_LIBS_HOME)\ifconsol.lib" \
	"$(oneAPI_LIBS_HOME)\libifcoremt.lib" \
	"$(oneAPI_LIBS_HOME)\libifport.lib" \
	"$(oneAPI_LIBS_HOME)\libircmt.lib" \
	"$(oneAPI_LIBS_HOME)\ifmodintr.lib" \
	"$(oneAPI_LIBS_HOME)\svml_dispmt.lib" \
	"$(oneAPI_LIBS_HOME)\libirc.lib" \
	"$(oneAPI_LIBS_HOME)\libmmt.lib"\
	"$(oneAPI_LIBS_HOME)\libiomp5md.lib" \
	"$(VS_LIBS_HOME)\libcmt.lib" \
	"$(VS_LIBS_HOME)\oldnames.lib" \
	"$(VS_LIBS_HOME)\libvcruntime.lib" \
	"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\um\x64\kernel32.Lib"\
	"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\ucrt\x64\libucrt.lib"\
	"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\um\x64\Uuid.Lib"\
	"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\um\x64\ImageHlp.Lib"  


MKL_INCLUDE = -I"$(oneMKL_HOME)\include\intel64\lp64" -I"$(oneMKL_HOME)\include"
CUDA_INCLUDE = -I"$(CUDA_HOME)\include"

LAPACK = "$(oneMKL_HOME)\lib\intel64\mkl_lapack95_lp64.lib" \
	"$(oneMKL_HOME)\lib\intel64\mkl_blas95_lp64.lib" \
	"$(oneMKL_HOME)\lib\intel64\mkl_blacs_intelmpi_lp64.lib" \
	"$(oneMKL_HOME)\lib\intel64\mkl_scalapack_lp64.lib" \
	"$(oneMKL_HOME)\lib\intel64\mkl_intel_lp64.lib" \
	"$(oneMKL_HOME)\lib\intel64\mkl_intel_thread.lib" \
	"$(oneMKL_HOME)\lib\intel64\mkl_sequential.lib" \
	"$(oneMKL_HOME)\lib\intel64\mkl_core.lib" 

CUDA_LIB = "$(CUDA_HOME)\lib\x64\cublas.lib" \
	"$(CUDA_HOME)\lib\x64\cuda.lib" \
	"$(CUDA_HOME)\lib\x64\cudart.lib" \
	"$(CUDA_HOME)\lib\x64\cusolver.lib" \
	"$(CUDA_HOME)\lib\x64\cusolverMg.lib" 

NC = nvcc
NCFLAGS = -arch=native -Xptxas -O3,-v -use_fast_math -Xcompiler /openmp
CC = $(oneAPI_PATH)\bin\icpx.exe
CCFLAGS = -arch=native -O3 -use_fast_math 
FC = $(oneAPI_PATH)\bin\ifx.exe
FFLAGS = /O3 /fp:fast /QxHost /Qmkl #-openmp

all : solDensMatrixFinalRound.dll check.exe

check.exe: readData.obj writeData.obj checkData.obj basicMathSubroutines.obj computeResidual.obj main.obj check_solver.obj
	$(FC) -o $@ $^ $(LAPACK) $(LINK_LIB) $(CUDA_LIB) 
readData.obj: readData.f90
	$(FC) -c $^ $(FFLAGS) -o $@
writeData.obj: writeData.f90
	$(FC) -c $^ $(FFLAGS) -o $@
checkData.obj: checkData.f90
	$(FC) -c $^ $(FFLAGS) -o $@
basicMathSubroutines.obj : basicMathSubroutines.f90
	$(FC) -c $^ $(FFLAGS) -o $@
computeResidual.obj : computeResidual.f90
	$(FC) -c $^ $(FFLAGS) -o $@
main.obj: main.f90
	$(FC) -c $^ $(FFLAGS) $(MKL_INCLUDE) -o $@

solDensMatrixFinalRound.dll: solDensMatrixFinalRound.obj final_solver.obj
	$(FC) /dll  $^ $(LAPACK) $(LINK_LIB) $(CUDA_LIB) -o $@
solDensMatrixFinalRound.obj: solDensMatrixFinalRound.f90
	$(FC) -c $^ $(FFLAGS) -o $@
final_solver.obj: final_solver.cu
	$(NC) -c $^ $(NCFLAGS) $(CUDA_INCLUDE) -DMAX_SIZE=5000 -DMAX_DELTA=300 -o $@
check_solver.obj: final_solver.cu
	$(NC) -c $^ $(NCFLAGS) $(CUDA_INCLUDE) -DMAX_SIZE=10000 -DMAX_DELTA=300 -DCHECK -o $@

clean:
	del *.obj *.mod *.exe *.o *.dll
